package com.fxpc.hddc.death.service.impl;

import com.fxpc.common.TaskInfoEnum;
import com.fxpc.common.basedivision.service.BaseDivisionService;
import com.fxpc.common.vo.DivisionParamVo;
import com.fxpc.common.vo.ScheduleInfoVo;
import com.fxpc.hddc.death.dto.pg.entity.FxCasualtyLevelMapPgEntity;
import com.fxpc.hddc.death.dto.primary.entity.FxCasualtyLevelMapEntity;
import com.fxpc.hddc.death.dto.primary.vo.FxCasualtyLevelMapVo;
import com.fxpc.hddc.death.jpa.dao.pg.FxCasualtyLevelMapPgBaseDao;
import com.fxpc.hddc.death.jpa.dao.primary.FxCasualtyLevelMapBaseDao;
import com.fxpc.hddc.death.service.FxCasualtyLevelMapService;
import com.fxpc.util.GetSpecificationUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("FxCasualtyLevelMap2ServiceImpl")
public class FxCasualtyLevelMap2ServiceImpl implements FxCasualtyLevelMapService {

    @Autowired
    private FxCasualtyLevelMapBaseDao baseDao;
    @Autowired
    private FxCasualtyLevelMapPgBaseDao pgBaseDao;
    @Autowired
    private BaseDivisionService dvisionService;

    @Value("${pc.scop}")
    private int scop;

    /**
     * 质检数据
     *
     * @return
     */
    @Override
    public List<ScheduleInfoVo> fxpcQualityData() {
        //当前时间
        LocalDateTime timeNow = dvisionService.getTimeNow();
        LocalDate nowDay = timeNow.toLocalDate();
        //LocalDate nowDay = LocalDate.parse("2021-03-18", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //查询MySQL数据库当日创建的数据个数
        //List<FxCasualtyLevelMapEntity> ativefaults = FxCasualtyLevelMapBaseDao.findOneDayDataByCreateTime(nowDay);
        List<FxCasualtyLevelMapEntity> ativefaults=null;
        if (0 == scop) {
            ativefaults = baseDao.findALlTime("2");
        } else {
            ativefaults = baseDao.findOneDayDataByCreateTime(nowDay,"2");
        }
        //当日新增质检数据为空
        if (CollectionUtils.isEmpty(ativefaults)) {
            return null;
        }
        Map<String, List<FxCasualtyLevelMapVo>> provinceMap = new HashMap<>();
        Map<String, List<FxCasualtyLevelMapVo>> cityMap = new HashMap<>();
        Map<String, List<FxCasualtyLevelMapVo>> areaMap = new HashMap<>();
        for (FxCasualtyLevelMapEntity ativefault : ativefaults) {
            FxCasualtyLevelMapVo ativefaultVo = new FxCasualtyLevelMapVo();
            BeanUtils.copyProperties(ativefault, ativefaultVo);
            if (StringUtils.isNotEmpty(ativefaultVo.getProvince())) {
                provinceMap = splitData(provinceMap, ativefaultVo.getProvince(), ativefaultVo);
            }
            if (StringUtils.isNotEmpty(ativefaultVo.getCity())) {
                cityMap = splitData(cityMap, ativefaultVo.getCity(), ativefaultVo);
            }
            if (StringUtils.isNotEmpty(ativefaultVo.getArea())) {
                areaMap = splitData(areaMap, ativefaultVo.getArea(), ativefaultVo);
            }
        }
        List<ScheduleInfoVo> ps = null;
        List<ScheduleInfoVo> cs = null;
        List<ScheduleInfoVo> as = null;
        if (!CollectionUtils.isEmpty(provinceMap)) {
            ps = resultScheduleInfo(provinceMap, 1);
        }
        if (!CollectionUtils.isEmpty(cityMap)) {
            cs = resultScheduleInfo(cityMap, 2);
        }
        if (!CollectionUtils.isEmpty(areaMap)) {
            as = resultScheduleInfo(areaMap, 3);
        }
        List<ScheduleInfoVo> result = new ArrayList<>();
        if(ps!=null){
            result.addAll(ps);
        }
        if(cs!=null){
            result.addAll(cs);
        }
        if(as!=null){
            result.addAll(as);
        }
        return result;
    }

    public List<ScheduleInfoVo> resultScheduleInfo(Map<String, List<FxCasualtyLevelMapVo>> map, int flag) {
        String p = null;
        String c = null;
        String a = null;
        List<ScheduleInfoVo> list = new ArrayList<>();
        for (String key : map.keySet()) {
            if (flag == 1) {
                p = key;
            } else if (flag == 2) {
                c = key;
            } else {
                a = key;
            }
            DivisionParamVo divisionParamVo = new DivisionParamVo(p, c, a);
            divisionParamVo.setExtend10("2");
            Specification<FxCasualtyLevelMapEntity> specification = GetSpecificationUtil.getSpecification1(divisionParamVo);
            Long count = baseDao.count(specification);
            int allCount = new Long(count).intValue();
            ScheduleInfoVo scheduleInfoVo = new ScheduleInfoVo();
            scheduleInfoVo.setId(TaskInfoEnum.DEATH_LEVELMAP2.getQtId());
            scheduleInfoVo.setFinishCount(allCount);
            scheduleInfoVo.setAllCount(allCount);
            scheduleInfoVo.setPercent(1.0f);
            String code = dvisionService.getQhCode(TaskInfoEnum.DEATH_LEVELMAP2.getTaskCode(), key, flag);
            scheduleInfoVo.setCode(code);
            list.add(scheduleInfoVo);
        }
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }

    /**
     * 上报数据
     *
     * @return
     */
    @Override
    public List<ScheduleInfoVo> fxpcReportData() {
        //当前时间
        LocalDateTime timeNow = dvisionService.getTimeNow();
        LocalDate nowDay = timeNow.toLocalDate();
        //LocalDate nowDay = LocalDate.parse("2021-03-18", DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        //当日上报数据
        //List<FxCasualtyLevelMapPgEntity> pgActivefaults = FxCasualtyLevelMapPgBaseDao.findPgOneDayDataByCreateTime(nowDay);
        List<FxCasualtyLevelMapPgEntity> pgActivefaults=null;
        if (0 == scop) {
            pgActivefaults = pgBaseDao.findAllTime("2");
        } else {
            pgActivefaults = pgBaseDao.findPgOneDayDataByCreateTime(nowDay,"2");
        }
        if (CollectionUtils.isEmpty(pgActivefaults)) {//如果上报数据为空
            return null;
        }
        Map<String, List<FxCasualtyLevelMapVo>> provincePgMap = new HashMap<>();
        Map<String, List<FxCasualtyLevelMapVo>> cityPgMap = new HashMap<>();
        Map<String, List<FxCasualtyLevelMapVo>> areaPgMap = new HashMap<>();
        for (FxCasualtyLevelMapPgEntity ativefault : pgActivefaults) {
            FxCasualtyLevelMapVo ativefaultVo = new FxCasualtyLevelMapVo();
            BeanUtils.copyProperties(ativefault, ativefaultVo);
            ativefaultVo.setArea(ativefault.getArea());
            if (StringUtils.isNotEmpty(ativefaultVo.getProvince())) {
                provincePgMap = splitData(provincePgMap, ativefaultVo.getProvince(), ativefaultVo);
            }
            if (StringUtils.isNotEmpty(ativefaultVo.getCity())) {
                cityPgMap = splitData(cityPgMap, ativefaultVo.getCity(), ativefaultVo);
            }
            if (StringUtils.isNotEmpty(ativefaultVo.getArea())) {
                areaPgMap = splitData(areaPgMap, ativefaultVo.getArea(), ativefaultVo);
            }
        }
        List<ScheduleInfoVo> ps = null;
        List<ScheduleInfoVo> cs = null;
        List<ScheduleInfoVo> as = null;
        if (!CollectionUtils.isEmpty(provincePgMap)) {
            ps = resultScheduleUpInfo(provincePgMap, 1);
        }
        if (!CollectionUtils.isEmpty(cityPgMap)) {
            cs = resultScheduleUpInfo(cityPgMap, 2);
        }
        if (!CollectionUtils.isEmpty(areaPgMap)) {
            as = resultScheduleUpInfo(areaPgMap, 3);
        }
        List<ScheduleInfoVo> result = new ArrayList<>();
        if(ps!=null){
            result.addAll(ps);
        }
        if(cs!=null){
            result.addAll(cs);
        }
        if(as!=null){
            result.addAll(as);
        }
        return result;
    }


    public List<ScheduleInfoVo> resultScheduleUpInfo(Map<String, List<FxCasualtyLevelMapVo>> map, int flag) {
        String p = null;
        String c = null;
        String a = null;
        List<ScheduleInfoVo> list = new ArrayList<>();
        for (String key : map.keySet()) {
            if (flag == 1) {
                p = key;
            } else if (flag == 2) {
                c = key;
            } else {
                a = key;
            }
            DivisionParamVo divisionParamVo = new DivisionParamVo(p, c, a);
            divisionParamVo.setExtend10("2");
            //质检数据
            Specification<FxCasualtyLevelMapEntity> specification = GetSpecificationUtil.getSpecification1(divisionParamVo);
            Long count = baseDao.count(specification);
            int allCount = new Long(count).intValue();
            //上报数据
            Specification<FxCasualtyLevelMapPgEntity> specificationPg = GetSpecificationUtil.getSpecification1(divisionParamVo);
            Long countPg = pgBaseDao.count(specificationPg);
            int finshCount = new Long(countPg).intValue();
            ScheduleInfoVo scheduleInfoVo = new ScheduleInfoVo();
            scheduleInfoVo.setId(TaskInfoEnum.DEATH_LEVELMAP2.getRpId());
            scheduleInfoVo.setFinishCount(finshCount);
            scheduleInfoVo.setAllCount(allCount);
            float percent = (float) finshCount / allCount;
            if(percent>1){
                percent=1f;
            }
            scheduleInfoVo.setPercent(percent);
            String code = dvisionService.getQhCode(TaskInfoEnum.DEATH_LEVELMAP2.getTaskCode(), key, flag);
            scheduleInfoVo.setCode(code);
            list.add(scheduleInfoVo);
        }
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }

    /**
     * 将数据经行省市区分类-同区域的数据放在一个map的key中，不同的key表示不同的区域，value表示该区域下的数据
     *
     * @param map
     * @param key
     * @param ativefaultVo
     * @return
     */
    public Map<String, List<FxCasualtyLevelMapVo>> splitData(Map<String, List<FxCasualtyLevelMapVo>> map, String key, FxCasualtyLevelMapVo ativefaultVo) {
        List<FxCasualtyLevelMapVo> list = map.get(key);
        if (CollectionUtils.isEmpty(list)) {
            List<FxCasualtyLevelMapVo> yhList = new ArrayList<>();
            yhList.add(ativefaultVo);
            map.put(key, yhList);
        } else {
            list.add(ativefaultVo);
            map.put(key, list);
        }
        return map;
    }

}
